<?php
/**
 * User:于小涛
 * Date:2020/2/17
 * Time:15:37
 */
namespace app\admin\controller\promotion;

use app\admin\BaseController;
use app\admin\model\rbac\Admin;
use app\admin\model\promotion\PromotionRecord as PromotionRecordModel;
use app\admin\model\system\Channel;
use app\admin\model\system\ChannelBindAdmin;
use think\App;
use think\facade\View;

class PromotionRecord extends BaseController {
    private $obj_promotion_record_model = null;
    private $obj_admin_model = null;
    private $obj_channel_bind_admin_model = null;
    private $obj_channel_model = null;
    private $arr_channel_list=[];
    private $arr_tuiguangzhuanyuan_list=[];
    private $arr_chuangyeguwen_list=[];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->obj_promotion_record_model = new PromotionRecordModel();
        $this->obj_channel_bind_admin_model = new ChannelBindAdmin();
        $this->obj_admin_model = new Admin();
        $this->obj_channel_model = new Channel();
        $this->arr_channel_list=$this->obj_channel_model->order('sort','desc')->select()->toArray();
        View::assign('arr_channel_list',$this->arr_channel_list);
        $this->arr_tuiguangzhuanyuan_list=$this->getAllAdmin(config('project.role_id.tuiguangzhuanyuan.id'));
        View::assign('arr_tuiguangzhuanyuan_list',$this->arr_tuiguangzhuanyuan_list);
        $this->arr_chuangyeguwen_list=$this->getAllAdmin(config('project.role_id.chuangyeguwen.id'));
        View::assign('arr_chuangyeguwen_list',$this->arr_chuangyeguwen_list);
    }

    public function getList(){
        return view();
    }

    public function getListData(){
        $arr_where=[];
        if($this->request->param('channel_id')){
            $arr_where[]=['c.id','=',$this->request->param('channel_id')];
        }
        if($this->request->param('admin_id')){
            $arr_where[]=['pr.tuiguang_admin_id','=',$this->request->param('tuiguang_admin_id')];
        }
        $arr_list=$this->obj_promotion_record_model
            ->alias('pr')
            ->field('pr.*,c.name as channel_name')
            ->leftjoin('channel c ','c.id=pr.channel_id')
            ->where($arr_where)
            ->order('pr.id','desc')
            ->paginate(['list_rows'=>$this->request->param('limit'),'page'=>$this->request->param('page')])
            ->toArray();
        foreach($arr_list['data'] as &$arr_item){
            $arr_item['tuiguang_admin_name']=$this->arr_tuiguangzhuanyuan_list[$arr_item['tuiguang_admin_id']]['name'];
            $arr_item['chuangye_admin_name']=empty($arr_item['chuangye_admin_id'])?'':$this->arr_chuangyeguwen_list[$arr_item['chuangye_admin_id']]['name'];
            $arr_item['valid_num']=empty($arr_item['chuangye_admin_id'])?'':$arr_item['valid_num'];
            //计算有效率
            if(empty($arr_item['chuangye_admin_id'])){
                $arr_item['valid_per']='';
            }else{
                if(empty($arr_item['all_num'])){
                    $arr_item['valid_per']=0;
                }else{
                    if(empty($arr_item['valid_num'])){
                        $arr_item['valid_per']='0.0%';
                    }else{
                        $arr_item['valid_per']=(number_format($arr_item['valid_num']/$arr_item['all_num'],3,'.','')*100).'%';
                    }
                }
            }
        }
        return output_page($arr_list);
    }

    public function edit(){
        if($this->request->param('name')){
            $arr_data=[
                'name'=>$this->request->param('name'),
                'sort'=>$this->request->param('sort'),
            ];
            $arr_re=$this->obj_promotion_record_model->edit($this->request->param('id'),$arr_data);
            if($arr_re['ret']==0){
                op_log('system','edit',"修改渠道成功，名称【{$this->request->param('name')}】");
                //删除所有的绑定
                $arr_del_bind_where=[
                    'channel_id'=>$this->request->param('id')
                ];
                $int_del_re=$this->obj_channel_bind_admin_model->where($arr_del_bind_where)->delete();
                if(!empty($this->request->param('admin_ids'))){
                    $arr_admin_ids=explode(',',$this->request->param('admin_ids'));
                    foreach($arr_admin_ids as $int_admin_id){
                        $arr_add_data=[
                            'admin_id'=>$int_admin_id,
                            'channel_id'=>$this->request->param('id')
                        ];
                        $int_num=$this->obj_channel_bind_admin_model->add($arr_add_data);
                    }
                }
                return output_data();
            }else{
                return output_data(array(),$arr_re['reason']);
            }
        }else{
            $arr_data_info=$this->obj_promotion_record_model->findOrEmpty($this->request->param('id'))->toArray();
            //获取每个渠道绑定的推广专员
            $arr_where=[
                'channel_id'=>$arr_data_info['id']
            ];
            $arr_channel_bind_admin_list=$this->obj_channel_bind_admin_model->where($arr_where)->select()->toArray();
            $arr_admin_ids=[];
            foreach($arr_channel_bind_admin_list as $arr_bind_item){
                $arr_admin_ids[]=$arr_bind_item['admin_id'];
            }
            $arr_data_info['admin_ids'] = implode(',',$arr_admin_ids);
            View::assign('arr_data_info',$arr_data_info);
            return view();
        }
    }

    public function del(){
        //获取详情
        $arr_info=$this->obj_promotion_record_model->findOrEmpty($this->request->param('id'))->toArray();
        $int_del_num=$this->obj_promotion_record_model->del($this->request->param('id'));
        $this->arr_channel_list=array_item_key_to_self_key($this->arr_channel_list,'id');
        if($int_del_num){
            op_log('promotion','del',"删除日期【{$arr_info['date']}】渠道【{$this->arr_channel_list[$arr_info['channel_id']]['name']}】推广员【{$this->arr_tuiguangzhuanyuan_list[$arr_info['tuiguang_admin_id']]['name']}】创业顾问【{$this->arr_chuangyeguwen_list[$arr_info['chuangye_admin_id']]['name']}】的推广数据");
            return output_data();
        }else{
            return output_data(array(),'error');
        }
    }
}